home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Do you have ever pass structures?
- Date: 21 Feb 1996 14:59:44 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gg850INNi4k@keats.ugrad.cs.ubc.ca>
- References: <4ge8mi$qjm@srvr1.engin.umich.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4ge8mi$qjm@srvr1.engin.umich.edu>,
- HASDI RODZMANN HASHIM <hasdi@news-server.engin.umich.edu> wrote:
- >As I gain more and more experience in writing C code (sure), I find
- >myself passing more and more information and returning even more.
- >This is where using structures become handy.
- >
- >MY QUESTION is do you ever pass the whole structures to a function
- >or you just pass a pointer to a function? For example, for a struct
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
- [ [ pass ]V [ a pointer ]NP [ to a function ]PP ]VP
-
- [ [ pass ]V [ a pointer [ to a function ]PP ]NP ]VP
-
- Ambigous natural language: I love it!
-
- >If I want to avoid bar from being modified, one limited way to do
- >it is to add the CONST qualifier.
- > void foo(const bar *a)
- >
- >So is there any REAL advantage is passing an entire structure? Do people
- >ever do it?
-
- The const qualifier makes no real guarantee that the object won't be modified,
- accidentally. In most C environments, you could still define a function foo()
- which clobbers the structure, and have it link in without a hitch.
-
- The idea passing structures by value is that you _can_ modify the parameter,
- _without_ affecting the argument. For small structures, it may be worth-while,
- if your function can benefit from these semantics.
-
- Structure passing is typically implemented by putting the whole damn thing on
- the stack, so it's probably not the best thing for large structures,
- particularly if a pointer will suffice.
-
- >I've some people's source code and almost always, they
- >pass pointers to structures instead of the structure itself. In a way,
-
- Some old compilers may not like structure passing. Also, passing pointers to
- structures makes sense when the called function is supposed to do something to
- the structure, which is often the case. For example, it would be no use passing
- a FILE structure to fread(), since fread() needs to update the fields of the
- FILE structure such as the buffer pointer.
-
- Pointers to structures are often used as handles for abstract data types. The
- code using the pointer may not even have a complete declaration for the
- structure, which is hidden in another module.
- --
-
-